package com.deluxelx.soul.inn.web.article;

import cn.hutool.core.collection.CollectionUtil;
import com.deluxelx.soul.inn.bean.article.ArticleDetailVO;
import com.deluxelx.soul.inn.bean.article.ArticleVO;
import com.deluxelx.soul.inn.common.param.CollectedParam;
import com.deluxelx.soul.inn.common.result.SoulInnResult;
import com.deluxelx.soul.inn.service.article.ArticleService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 文章相关Controller
 *
 * @author liaozihao
 * @date 2024-03-22 16:18
 */
@RestController
@RequestMapping("/soul/inn/article")
public class ArticleController {

    @Resource
    private ArticleService articleService;

    @GetMapping("/fetchByCondition")
    public SoulInnResult<List<ArticleVO>> fetchArticleListByType (String typeId, Integer limit) {
        List<ArticleVO> articleVOS = articleService.fetchArticleListByCondition(typeId, limit);
        if (CollectionUtil.isNotEmpty(articleVOS)) {
            return SoulInnResult.success(articleVOS);
        }
        return SoulInnResult.fail("根据导航栏类型查询文章列表失败");
    }

    @GetMapping("/articleDetail")
    public SoulInnResult<ArticleDetailVO> fetchArticleDetail (String id) {
        ArticleDetailVO articleDetailVO = articleService.fetchArticleDetail(id);
        if (articleDetailVO != null) {
            return SoulInnResult.success(articleDetailVO);
        }
        return SoulInnResult.fail("查询文章详情失败");
    }

    @PostMapping("/isCollected")
    public SoulInnResult<Boolean> isCollected(@RequestBody CollectedParam param) {
        try {
            Boolean isCollected = articleService.isCollectArticle(param);
            return SoulInnResult.success(isCollected);
        } catch (Exception e) {
            return SoulInnResult.fail("获取收藏状态失败！");
        }
    }

    @PostMapping("/followArticle")
    public SoulInnResult<Boolean> followArticle(@RequestBody CollectedParam param) {
        try {
            Boolean isSuccess = articleService.followArticle(param);
            return SoulInnResult.success(isSuccess);
        } catch (Exception e) {
            return SoulInnResult.fail("关注失败！");
        }
    }

    @PostMapping("/unFollowArticle")
    public SoulInnResult<Boolean> unFollowArticle(@RequestBody CollectedParam param) {
        try {
            Boolean isSuccess = articleService.unFollowArticle(param);
            return SoulInnResult.success(isSuccess);
        } catch (Exception e) {
            return SoulInnResult.fail("取消关注失败！");
        }
    }

}
